﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Drawing;

namespace StoneFish
{
    public static class Extensions
    {
        static string START_TOKEN = "$=";
        static string END_TOKEN = "=$";
        static char ZERO_BIT = '0';

        /// <summary>
        /// Converts a string to en equivelant string of bits
        /// </summary>
        /// <returns>string representing the text in binary format</returns>
        public static string ToBitString(this string PlainText)
        {
            string CharBits;
            StringBuilder BitStringBldr = new StringBuilder(PlainText.Length * 8);
            for (int CharCount = 0; CharCount < PlainText.Length; CharCount++)
            {
                CharBits = Convert.ToString(PlainText[CharCount], 2);
                while(CharBits.Length<8) CharBits= ZERO_BIT +CharBits;
                BitStringBldr.Append(CharBits);
            }
          return BitStringBldr.ToString();
        }


        /// <summary>
        /// encapsulates the text with start and end tokens*/
        /// </summary>
        /// <param name="PlainText"></param>
        /// <returns>string representing encapsulated text</returns>
        public static string Encapsulate(this string PlainText)
        {
            return (START_TOKEN + PlainText + END_TOKEN);
        }

        /// <summary>
        /// decapsulates the string by removing the start and end tokens
        /// </summary>
        /// <param name="PlainText"></param>
        /// <returns>string representing the decapsulated text</returns>

        public static string Decapsulate(this string PlainText)
        {
            PlainText = PlainText.Replace(START_TOKEN, String.Empty);
            PlainText = PlainText.Replace(END_TOKEN, String.Empty);
            return PlainText;
        }

        /// <summary>
        /// checks whether the text header is correctly formatted
        /// </summary>
        /// <param name="text"></param>
        /// <returns>Boolean represneting the validation result</returns>
        public static bool isValidStart(this string text)
        {
            return text.StartsWith(START_TOKEN);
        }

        /// <summary>
        /// checks whether the text has reached its end
        /// </summary>
        /// <param name="text"></param>
        /// <returns>Boolean represneting the validation result</returns>
        public static bool isValidEnd(this string text)
        {
            return text.EndsWith(END_TOKEN);
        }


    }
}
